<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\LuckMoneyRule;
use App\Models\OpenCity;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class LuckMoneyRuleController extends Controller
{
    use HasResourceActions;
    private $name = '红包整体规则';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new LuckMoneyRule);

        $grid->id('Id')->expand(function ($model) {
            $timeRule = $model->timerule()->get()->map(function ($rule) {
                return $rule->only(['id', 'start', 'end', 'lower', 'upper']);
            });
            return new Table(['ID', '开始次数', '结束次数', '下限金额(分)', '上限金额(分)'], $timeRule->toArray());
        });
        $grid->cityinfo()->name('城市');
        $grid->sdate('开始日期');
        $grid->edate('结束日期');
        $grid->stime('开始时间');
        $grid->etime('结束时间');
        $grid->is_on('是否开启')->display(function ($isOn) {
            return $isOn ? '<span style="color:blue">开启</span>' : '<span style="color:red">关闭</span>';
        });
        $grid->daily_user_upper('每日用户上限(分)');
        $grid->total_upper('总上限金额(分)');
        $grid->current_amount('当前金额(分)');
        $grid->interval('抽奖间隔(秒)');
        $grid->settle_inv('结算间隔(秒)');
        $grid->min_amount('最小金额(分)');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(LuckMoneyRule::findOrFail($id));

        $show->id('Id');
        $show->cityinfo('城市名称')->unescape()->as(function ($city) {
            return $city->name;
        });
        $show->sdate('开始日期');
        $show->edate('结束日期');
        $show->stime('开始时间');
        $show->etime('结束时间');
        $show->is_on('是否开启');
        $show->daily_user_upper('每日用户上限(分)');
        $show->total_upper('总上限金额(分)');
        $show->current_amount('当前金额(分)');
        $show->interval('抽奖间隔(秒)');
        $show->settle_inv('结算间隔(秒)');
        $show->min_amount('最小金额(分)');

        $show->timerule('单次规则', function ($time) {
            $time->setResource('/admin/luckmoneytimerule');
            $time->start("开始次数");
            $time->end("结束次数");
            $time->lower("下限金额(分)");
            $time->upper("上限金额(分)");

            $time->disableExport();
            $time->disableRowSelector();
            $time->actions(function ($actions) {
                $actions->disableView();
            });

        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new LuckMoneyRule);
        $cityList = OpenCity::getCityList();
        $cityList['000000'] = '全国';

        $form->select('city', '地区')->options($cityList)->rules('required');
        $form->date('sdate', '开始日期')->default(date('Y-m-d'))->rules('required');
        $form->date('edate', '结束日期')->default(date('Y-m-d'))->rules('required');
        $form->time('stime', '开始时间')->default(date('H:i:s'))->rules('required');
        $form->time('etime', '结束时间')->default(date('H:i:s'))->rules('required');
        $states = [
            'on' => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('is_on', '是否开启')->states($states);

        $form->number('daily_user_upper', '每日用户上限(分)')->rules('required');
        $form->number('total_upper', '总上限金额(分)')->rules('required');
        $form->number('interval', '抽奖间隔(秒)')->rules('required');
        $form->number('settle_inv', '结算间隔(秒)')->rules('required');
        $form->number('min_amount', '最小金额(分)')->rules('required');
        $form->image('bg', '红包背景');
        $form->text('desc', '红包说明')->default('领取乒乓江湖红包')->rules('required');

        return $form;
    }
}
